package Love.ChristmasTree;

import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

public class MyPanel extends JPanel implements ActionListener{
    int x, y ;
    JButton onOff ;
    Timer time ;
    boolean flag ;
    boolean color ;
    File file = new File("/星星.png") ;
    URL url = null;
    URI uri = null ;
    ImageIcon icon;
    AudioClip clip = null;
    MyPanel() {
        setLayout(null);
        onOff = new JButton() ;
        icon = new ImageIcon("/星星.png");
        icon.setImage(icon.getImage().getScaledInstance(50,50, 0)) ;
        onOff.addActionListener(this) ;
        onOff.setBorder(null) ;
        onOff.setContentAreaFilled(false) ;
        onOff.setBounds(0, 0, 50, 50) ;
        this.add(onOff);
        flag = true ;
        color = true ;
        time = new Timer(300,this) ;
        time.stop() ;
        try {
            uri=file.toURI();
            url = uri.toURL() ;
        }
        catch (MalformedURLException e1) {}
        clip=Applet.newAudioClip(url);
    }
    public void paintComponent(Graphics g) {
        x = 380 ;
        y = 100 ;

        Color red = new Color(0, 255, 102) ;
        Color yellow = new Color(255, 241, 0) ;
        Color blue = new Color(0x2BB1EF) ;
        Color happy = new Color(0xAEF607);
        Color happies = new Color(0xF1C4F1) ;
        Color top1 = new Color(0xFDFA6F8B, true);
        Color top2 = new Color(0x9C44F5);

        if(color) {
            drawDecoration(x, y-25, 0, top1, g);
        }
        else {
            drawDecoration(x, y-25, 0, top2, g);
        }
        drawTree(1, 4, g) ;
        if(color) {
            drawDecoration(x+22, y-44, 6, yellow, g);
            drawDecoration(x, y-22, 8, red, g);
        }
        else {
            drawDecoration(x+22, y-44, 6, red, g);
            drawDecoration(x, y-22, 8, blue, g);
        }
        x = 380-2*22;
        drawTree(3, 6, g) ;
        if(color) {
            drawDecoration(x+22, y-44, 10, blue, g);
            drawDecoration(x, y-22, 12, red, g);
        }
        else {
            drawDecoration(x+22, y-44, 10, red, g);
            drawDecoration(x, y-22, 12, yellow, g);
        }
        x = 380-4*22;
        drawTree(5, 8, g) ;
        if(color) {
            drawDecoration(x+66, y-88, 10, yellow, g);
            drawDecoration(x+44, y-66, 12, red, g);
            drawDecoration(x+22, y-44, 14, blue, g);
            drawDecoration(x, y-22, 16, yellow, g);
        }
        else {
            drawDecoration(x+66, y-88, 10, blue, g);
            drawDecoration(x+44, y-66, 12, yellow, g);
            drawDecoration(x+22, y-44, 14, red, g);
            drawDecoration(x, y-22, 16, blue, g);
        }
        x = 380-1*22;
        drwaRoot(g) ;
        drwafloor(g) ;

        //love
        if(color){
            //L
            for(x=60,y=70;x>20&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happy, g);
            }
            for(x=20;x<80;x++){
                drawDecoration(x, 150, 0, happy, g);
            }

            //O
            for(x=170,y=70;x>130&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happy, g);
            }
            for(x=230,y=70;x>190&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happy, g);
            }
            for(x=230;x>170;x--){
                drawDecoration(x, 70, 0, happy, g);
            }
            for(x=190;x>130;x--){
                drawDecoration(x, 150, 0, happy, g);
            }

            //V
            for(x=20,y=200;x<60&&y<280;x++,y+=3){
                drawDecoration(x, y, 0, happy, g);
            }
            for(x=80,y=200;x>20&&y<280;x--,y+=3){
                drawDecoration(x, y, 0, happy, g);
            }

            //E
            for(x=230;x>170;x--){
                drawDecoration(x, 200, 0, happy, g);
            }
            for(x=210;x>150;x--){
                drawDecoration(x, 240, 0, happy, g);
            }
            for(x=190;x>130;x--){
                drawDecoration(x, 280, 0, happy, g);
            }
            for(x=170,y=200;x>130&&y<280;x--,y+=2){
                drawDecoration(x, y, 0, happy, g);
            }
        }else{
            //L
            for(x=70,y=70;x>30&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happies, g);
            }
            for(x=30;x<90;x++){
                drawDecoration(x, 150, 0, happies, g);
            }

            //O
            for(x=180,y=70;x>140&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happies, g);
            }
            for(x=240,y=70;x>200&&y<150;x--,y+=2){
                drawDecoration(x, y, 0, happies, g);
            }
            for(x=240;x>180;x--){
                drawDecoration(x, 70, 0, happies, g);
            }
            for(x=200;x>140;x--){
                drawDecoration(x, 150, 0, happies, g);
            }

            //V
            for(x=30,y=200;x<70&&y<280;x++,y+=3){
                drawDecoration(x, y, 0, happies, g);
            }
            for(x=90,y=200;x>30&&y<280;x--,y+=3){
                drawDecoration(x, y, 0, happies, g);
            }

            //E
            for(x=240;x>180;x--){
                drawDecoration(x, 200, 0, happies, g);
            }
            for(x=220;x>160;x--){
                drawDecoration(x, 240, 0, happies, g);
            }
            for(x=200;x>140;x--){
                drawDecoration(x, 280, 0, happies, g);
            }
            for(x=180,y=200;x>140&&y<280;x--,y+=2){
                drawDecoration(x, y, 0, happies, g);
            }
        }



        //祝福语
        if (color){
            g.setColor(Color.pink);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 35));
            g.drawString("亲爱的小宝,圣诞将至，提前送你棵圣诞树！",100,535);
            g.drawString("相逢是首悠扬的歌，相识是杯醇香的酒，",130,580);
            g.drawString("相处是那南飞的雁，相知是跟古老的藤。",160,625);
            g.drawString("嬉闹是彼此最甜蜜的瞬间，愿幸福与平安永远伴随你！圣诞快乐！！",190,670);
            g.drawString("愿所有的期待和梦想都能如期而至,每一刻都是美好的时光！",220,715);
            g.drawString("爱你的大宝",1000,750);
        }


        //爱
        if (color) {
            g.setColor(Color.pink);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 65));
            g.drawString("昕雅小宝贝",420,65);
            g.drawString("臭小宝",920,65);
            g.drawString("/(≧ x ≦)\\", 50, 452);
        }else {
            g.setColor(top1);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 65));
            g.drawString("昕雅小宝贝",420,65);
            g.drawString("臭小宝",920,65);
            g.drawString("/(≧ x ≦)\\", 50, 452);
        }




        if (color){
            g.setColor(Color.pink);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 40));
            g.drawString("笨蛋小宝",920,535);
            JLabel jLabel1 = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\爱心.png"));
            JLabel jLabel2 = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\爱心.png"));
            jLabel1.setBounds(895,555,40,40);
            jLabel2.setBounds(1140,555,40,40);
            this.add(jLabel1);
            this.add(jLabel2);
            g.drawString("I LOVE You",935,585);
            g.drawString(" Forever ! ",965,635);
        }else {
            g.setColor(top1);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 40));
            g.drawString("笨蛋小宝",920,535);
            JLabel jLabel1 = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\爱心.png"));
            JLabel jLabel2 = new JLabel(new ImageIcon("D:\\Java\\Love\\src\\Love\\ChristmasTree\\爱心.png"));
            jLabel1.setBounds(895,555,40,40);
            jLabel2.setBounds(1140,555,40,40);
            this.add(jLabel1);
            this.add(jLabel2);
            g.drawString("I LOVE You",935,585);
            g.drawString(" Forever ! ",965,635);
        }



        //兔子
        if(color){
            g.setColor(red);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 25));
            g.drawString("╭╮╭╮                 ╭╮╭╮", 725, 392);
            g.drawString("╭－┴┴★╮        ╭－┴┴★╮", 702, 412);
            g.drawString("│◎　  ︵ │         │ ︵　  ◎ │", 699, 432);
            g.drawString("╰○－－○╯        ╰○－－○╯", 696, 452);
        }else{
            g.setColor(top2);
            g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 25));
            g.drawString("╭╮╭╮                 ╭╮╭╮", 725, 392);
            g.drawString("╭－┴┴★╮        ╭－┴┴★╮", 702, 412);
            g.drawString("│◎　  ︵ │         │ ︵　  ◎ │", 699, 432);
            g.drawString("╰○－－○╯        ╰○－－○╯", 696, 452);
        }

        if(color){
            drawWrite(500,150,0, happy,g);
        }else {
            drawWrite(502,152,0, top1,g);
        }

        if(color){
            drawPhone(550,250,0, happy,g);
            drawPig(600,350,0, happy,g);
        }else {
            drawPhone(552,252,0, top2,g);
            drawPig(602,352,0, top2,g);
        }

    }
    void drawTree(int from, int to, Graphics g)	{
        Color c = new Color(9, 124, 37) ;
        g.setColor(c) ;
        for(int i=from; i<=to; i++) {
            for(int j=0; j<(i*2-1); j++) {
                g.fillRect(x, y, 20, 20);
                x += 22 ;
            }
            x = 380-i*22 ;
            y += 22 ;
        }
    }
    void drawDecoration(int tx, int ty, int num, Color c, Graphics g) {
        g.setColor(c) ;
        g.fillRoundRect(tx, ty, 18, 18, 18, 18) ;
        g.fillRoundRect(tx+num*22, ty, 18, 18, 18, 18) ;
    }

    void drawWrite(int tx, int ty, int num, Color c, Graphics g) {
        g.setColor(c);
        g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 100));
        g.drawString("Merry Christmas", tx, ty);
    }

    void drawPhone(int tx, int ty, int num, Color c, Graphics g) {
        g.setColor(c);
        g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 50));
        g.drawString("( ￣ ▽￣)o╭╯☆#╰ _─﹏─)╯ ", tx, ty);
    }

    void drawPig(int tx, int ty, int num, Color c, Graphics g) {
        g.setColor(c);
        g.setFont(new Font("方正舒体", Font.HANGING_BASELINE, 50));
        g.drawString("小宝，快看！！兔子！！", tx, ty);
    }

    void drwaRoot(Graphics g) {
        Color c = new Color(131, 78, 0) ;
        g.setColor(c);
        for(int i=0; i<4; i++) {
            for(int j=0; j<3; j++) {
                g.fillRect(x, y, 20, 20);
                x += 22 ;
            }
            x = 380-1*22 ;
            y += 22 ;
        }
    }
    void drwafloor(Graphics g) {
        Color c = new Color(0x5BAA0B) ;
        g.setColor(c);
        for(int i=0; i<2; i++) {
            x=0;
            for(int j=0; j<60; j++) {
                g.fillRect(x, y, 20, 20);
                x += 22 ;
            }
            y += 22 ;
        }
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == onOff) {
            if(flag) {
                ImageIcon icon = new ImageIcon("/星星.png");
                icon.setImage(icon.getImage().getScaledInstance(50,50, 0)) ;
                flag = false ;
                clip.loop();
                time.restart() ;
            }
            else {
                ImageIcon icon = new ImageIcon("/星星.png");
                icon.setImage(icon.getImage().getScaledInstance(50,50, 0)) ;
                flag = true ;
                time.stop() ;
                clip.stop() ;
            }
        }
        else if(e.getSource() == time) {
            repaint() ;
            color = !color ;
        }
    }

}
